#include "game.h"
#include <iostream>

using namespace std;

void Game::newGame(void)
{
	// starts the game
	int turn, color, difficulty, inputCh, startNew;
	bool done = false;
	MEVENT event;
	// an infinite loop, the program ends when the user presses the quit
	// button and exit is called
	while(!done) {
		// red always goes first
		turn = 0;
		// color is set to -1, then chosen later
		color = -1;
		// clear the screen, initialize the board, and print it
		clear();
		gameBoard.initializeBoard();
		gameBoard.printAll();
		// turn off the new game flag
		gameBoard.setNewGame(false);
		// choose a color
		while(color == -1) {
			// print buttons to choose from
			mvprintw(10, 2, " Choose a Color ");
			attron(COLOR_PAIR(1));
			mvprintw(12, 4, " Red ");
			attroff(COLOR_PAIR(1));
			attron(COLOR_PAIR(3));
			mvprintw(12, 10, " Blue ");
			attroff(COLOR_PAIR(3));
			refresh();
			// get the user input
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(startNew = gameBoard.checkMenu(event.x, event.y)) {
							if(startNew == 1) {
								// if the New Game button was pressed, set the flag
								gameBoard.setNewGame(true);
							}
						} else if(event.x > 4 && event.x < 9 && event.y == 12) {
							// if the Red button was pressed, the user is red
							color = 0;
						} else if(event.x > 9 && event.x < 16 && event.y == 12) {
							// if the Blue button was pressed, the user is blue
							color = 1;
						}
					}
				}
			}
		}

		// clear the area under the menu buttons
		mvprintw(10, 2, "                ");
		mvprintw(12, 4, "            ");
		refresh();
		difficulty = 0; // might add code to choose a difficulty if we have multiple difficulties
		// set the colors, difficulty
		gameBoard.setUserColor(color);
		user.setColor(color);
		comp.setColor((color + 1)%2);
		comp.setDiff(difficulty);
		// clear what the computer knows about your pieces
		comp.clearKnown();
		// have the computer set up its pieces
		srand(time(0));
		int randnum = rand()%3+1;
		comp.aiSetup(gameBoard, randnum);	// Setup computer board, select setup style (1)
		// print the board, etc.
		gameBoard.printAll();
		// have the user set up the pieces
		user.setup(gameBoard);
		while(!gameBoard.isDone(!(turn==color)) && !gameBoard.getNewGame()) {
			// while the game is not over, and the New Game button has not been pressed
			if(turn == color) {
				// if it is the user's turn, wait for him to move a piece
				user.makeMove(gameBoard);
			} else {
				gameBoard.printAll();
				// if it is the computer's turn, have him move a piece
				comp.makeMove(gameBoard);
				//comp.simpleAI(gameBoard);
			}
			// change to the next turn
			turn = (turn + 1)%2;
		}
		// if the game ended normally (the New Game button was not pressed
		while(color != -1 && !gameBoard.getNewGame()) {
			// displays the winner until the New Game button is pressed
			// whoever's turn it was lost, because the other player captured their flag or they could not move
			if(turn != color) {
				mvprintw(10, 2, "    You Win!    ");
				refresh();
			} else {
				mvprintw(10, 2, "    You Lose    ");
				refresh();
			}

			// get user input
			inputCh = getch();
			if(inputCh == KEY_MOUSE) {
				if(getmouse(&event) == OK) {		  
					if(event.bstate & BUTTON1_CLICKED) {
						if(startNew = gameBoard.checkMenu(event.x, event.y)) {
							// check the menu buttons
							if(startNew == 1) {
								// if the New Game button was pressed, exit the loop
								color = -1;
							}
						}
					}
				}
			}
		}

	}
	return;
}

